package com.ruoyi.user.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.domain.TbPosts;
import com.ruoyi.user.service.ITbPostsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 岗位管理Controller
 * 
 * @author ruoyi
 * @date 2024-10-08
 */
@RestController
@RequestMapping("/user/posts")
public class TbPostsController extends BaseController
{
    @Autowired
    private ITbPostsService tbPostsService;

    /**
     * 查询岗位管理列表
     */
    @PreAuthorize("@ss.hasPermi('user:posts:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbPosts tbPosts)
    {
        startPage();
        List<TbPosts> list = tbPostsService.selectTbPostsList(tbPosts);
        return getDataTable(list);
    }

    /**
     * 导出岗位管理列表
     */
    @PreAuthorize("@ss.hasPermi('user:posts:export')")
    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbPosts tbPosts)
    {
        List<TbPosts> list = tbPostsService.selectTbPostsList(tbPosts);
        ExcelUtil<TbPosts> util = new ExcelUtil<TbPosts>(TbPosts.class);
        util.exportExcel(response, list, "岗位管理数据");
    }

    /**
     * 获取岗位管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:posts:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable("postId") Long postId)
    {
        return success(tbPostsService.selectTbPostsByPostId(postId));
    }

    /**
     * 新增岗位管理
     */
    @PreAuthorize("@ss.hasPermi('user:posts:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbPosts tbPosts)
    {
        return toAjax(tbPostsService.insertTbPosts(tbPosts));
    }

    /**
     * 修改岗位管理
     */
    @PreAuthorize("@ss.hasPermi('user:posts:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbPosts tbPosts)
    {
        return toAjax(tbPostsService.updateTbPosts(tbPosts));
    }

    /**
     * 删除岗位管理
     */
    @PreAuthorize("@ss.hasPermi('user:posts:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {
        return toAjax(tbPostsService.deleteTbPostsByPostIds(postIds));
    }

    /**
     * 获取所有有效岗位
     */
    @GetMapping("/listAll")
    public AjaxResult listAllActivePosts()
    {
        TbPosts query = new TbPosts();
        query.setState(1); // 确保这里的状态值正确对应于有效岗位的状态
        List<TbPosts> list = tbPostsService.selectTbPostsList(query);
        return AjaxResult.success(list);
    }
}
